import {Component, OnInit, ViewEncapsulation, ViewChild, ElementRef, OnDestroy} from '@angular/core';
import {Content} from "@shared/components/toolbar/toolbar-content";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ToolsService} from "@shared/service/tools/tools.service";
import {OrderService} from "@order/order.service";

import {ActivatedRoute, ParamMap, Params, Router} from "@angular/router";
import {GoodsOrderRespVO} from "@interface/index";



@Component({
  selector: 'seed-order-list',
  templateUrl: './order-list.component.html',
  styleUrls: ['./order-list.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class OrderListComponent implements OnInit, OnDestroy {

  public statusList: any = [
    {name: '全部', statusCode: '', hash: 'all'},
    {name: '待完善', statusCode: 9, hash: 'incomplete'},
    {name: '待付款', statusCode: 1, hash: 'waitPay'},
    {name: '已付款', statusCode: 2, hash: 'paid'},
    {name: '已完成', statusCode: 4, hash: 'completed'}
  ];
  public params: any = {
    'status': '',
    'page': 1,
    'rows': 10
  };
  public isActive: number | string = 0; // 状态选择序号
  public orderList: Array<GoodsOrderRespVO> = [];
  public ngUnsubscrible: any;
  @ViewChild('orderNav') orderNav: ElementRef;
  @ViewChild(Content) content;
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(
    private orderService: OrderService,
    private toolsService: ToolsService,
    private route: ActivatedRoute,
    private router: Router,
  ) {
    this.route.paramMap.subscribe((params: ParamMap) => {
      const tabCode = params.get('tabCode');
      if (tabCode) {
        this.handleHashChangeStatus(tabCode);
      }
    });
  }

  /**
   * @description 处理不同状态下的订单
   * @author @chenwh576
   * @date 20171021
   * @param idx
   * @param statusCode
   */
  handleChangeStatus(idx, statusCode) {
    this.isActive = idx;
    this.params['page'] = 1;
    this.params['status'] = statusCode;
    this.orderList = [];
    this.router.navigate(['/order', statusCode]);
    // this.infiniteScroll && this.infiniteScroll.enable(true);
    // this.getOrderList();
  }

  /**
   * @description 处理不同状态下的订单
   * @author @chenwh576
   * @date 20171021
   * @param idx
   * @param statusCode
   */
  handleHashChangeStatus(hash) {
    this.statusList.forEach((item, index) => {
      if (item['hash'] === hash) {
        this.isActive = index;
        this.params['page'] = 1;
        this.params['status'] = item['statusCode'];
        this.getOrderList();
      }
    });
  }

  /**
   * @description 获取订单列表list
   * @author @chenwh576
   * @date 20171021
   */
  getOrderList() {
    this.ngUnsubscrible = this.orderService.getOrderList(this.params).subscribe((res) => {
      if (res.result === '0' && res.data && res.data.list.length > 0) {
        this.params['page']++;
        this.infiniteScroll.complete();
        if (res.data.hasNextPage === false) {
          this.infiniteScroll.enable(false);
        }
        this.orderList = [...this.orderList, ...res.data.list];
      }
    });
  }


  ngOnInit() {
    // this.handleChangeStatus(0, '');
  }

  ngOnDestroy() {
    this.ngUnsubscrible && this.ngUnsubscrible.unsubscribe();
  }

}
